package com.alex.cms.web.controllers;

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alex.cms.domain.CmsSlide;
import com.alex.cms.domain.CmsUser;
import com.alex.cms.service.ICmsSlideService;
import com.alex.cms.web.constants.CMSConstants;
import com.alex.cms.web.vo.ResultVo;
import com.bawei.common.utils.StringUtil;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2019-09-11
 */
@Controller
@RequestMapping("/slide")
public class CmsSlideController {

	@Autowired
	private ICmsSlideService slideService;
	/**
	 * 
	* @Title: showAddPage
	* @Description: 跳转到添加页面
	* @return
	* @return ModelAndView
	 */
	@RequestMapping("/toAddSlide")
	public ModelAndView showAddPage() {
		ModelAndView mav = new ModelAndView("admin/slide-edit");
		CmsSlide slide = new CmsSlide();
		mav.addObject("slide", slide);
		return mav;
	}
	/**
	 * 
	* @Title: addSlide
	* @Description: 广告添加/修改共用
	* @param slide
	* @param file
	* @param session
	* @return
	* @return ModelAndView
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView addSlide(CmsSlide slide, MultipartFile file, HttpSession session) {
		ModelAndView mav = new ModelAndView("redirect:/admin/slides");
		String message = "";
		CmsSlide cmsSlide = new CmsSlide();
		CmsUser user = (CmsUser) session.getAttribute(CMSConstants.LOGIN_USER);
		if (user == null) {
			message = CMSConstants.USER_DO_NOT_LOGIN;
			mav.setViewName("redirect:/user/login");
			mav.addObject("message", message);
			return mav;
		}
		if (file.isEmpty()) {
			if(StringUtil.isEmpty(slide.getPicture())){
				cmsSlide.setPicture("");
			}
		} else {
			String fileName = file.getOriginalFilename();
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			String filePath = session.getServletContext().getRealPath("/") + CMSConstants.FILE_SAVE_PATH;
			String targetFileName = UUID.randomUUID().toString() + suffix;
			File file2 = new File(filePath);
			if (!file2.exists()) {
				file2.mkdir();
			}
			try {
				file.transferTo(new File(filePath + targetFileName));
			} catch (Exception e) {
				e.printStackTrace();
				message = CMSConstants.FILE_UPLOAD_ERROR;
				mav.addObject("slide", slide);
				mav.addObject("message", message);
				mav.setViewName("redirect:/slide/edit");
				return mav;
			}
			cmsSlide.setPicture(CMSConstants.FILE_SAVE_PATH + targetFileName);
		}
		
		cmsSlide.setTitle(slide.getTitle());
		if (StringUtil.isUrl(slide.getUrl())) {
			cmsSlide.setUrl(slide.getUrl());
		}
		if (slide.getId() == null) {
			slideService.saveOrUpdate(cmsSlide);
		}else{
			cmsSlide.setId(slide.getId());
			slideService.updateById(cmsSlide);
		}

		return mav;

	}
	/**
	 * 
	* @Title: getEditSlide
	* @Description: 跳转到修改页面
	* @param slideId
	* @return
	* @return ModelAndView
	 */
	@RequestMapping("/toEditslide")
	public ModelAndView getEditSlide(@RequestParam("slideId") Integer slideId) {
		ModelAndView mav = new ModelAndView("admin/slide-edit");
		CmsSlide slide = slideService.getById(slideId);
		mav.addObject("slide", slide);
		return mav;
	}
	
	@RequestMapping("/removeSlide")
	@ResponseBody
	public ResultVo remove(@RequestParam("id")Integer id){
		ResultVo result=new ResultVo();
		boolean ret =slideService.removeById(id);
		result.setStatus(ret == true?CMSConstants.RESULT_STATUS_SUCCESS:CMSConstants.RESULT_STATUS_ERROR);
		result.setMessage(ret==true?CMSConstants.RESULT_DELETE_MESSAGE_SUCCESS:CMSConstants.RESULT_DELETE_MESSAGE_ERROR);
		return result;
	}
}
